package com.smg.community.service;

import java.security.MessageDigest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jpush.api.JPushClient;
import cn.jpush.api.MessageResult;

@Service("talkService")
public class TalkService {

	@Transactional
	public MessageResult sendmsg(String uuid,String alias, String title, String content)
			throws Exception {

		// 对android和ios设备发送
		JPushClient jpush = new JPushClient("a46a8f2d44e9cfa4967c46f3",
				"51e9349aada97557b5456b8a");
		//需要把聊天内容插入到数据库中,
		int no=1;//数据库中的纪录主键
		//返回这条纪录的主键
		System.out.println("addFriend content :"+content);
		return jpush.sendCustomMessageWithAlias(no,uuid+alias, title,
				content);
//		if (result.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
//			System.out.println("成功");
//		}
	}

	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
			"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };

	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public String toMD5(String origin) {
		String resultString = null;
		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString
					.getBytes()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return resultString;
	}

}
